package com.yunji.ylexpiration.Controller;

import com.yunji.ylexpiration.entity.FurnCommodity;
import com.yunji.ylexpiration.entity.FurnUser;
import com.yunji.ylexpiration.service.FurnCommodityService;
import com.yunji.ylexpiration.service.RegisterService;
import com.yunji.ylexpiration.util.SimpleResponse;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.SimpleTimeZone;

@Controller
public class registerController {
@Autowired
    private RegisterService registerService;
    @Autowired
    private FurnCommodityService furnCommodityService;
/*
登录
 */
    @GetMapping("login.html")
    public String login(){
        return "login";
    }

    /**
     * 个人信息
     * @return
     */
    @GetMapping("/mygrxx.html")
    public String mygrxx(HttpSession session, Model model){
        FurnUser userid = registerService.selectByuserid(Integer.valueOf((int) session.getAttribute("userid")));
        if(userid!=null){
            model.addAttribute("mygrxx",userid);
        }
        return "mygrxx";
    }

    /**
     * 上传头像
     * @return
     */
    @PostMapping("/upload")
    public String shangchuang(@RequestParam("file1")MultipartFile file,String imgtx,HttpSession session){
        //上传文件
        FurnUser furnUser = new FurnUser();
        String fileName = file.getOriginalFilename();
        String filePath = "C:\\Users\\sss\\Desktop\\123\\ylexpiration\\src\\main\\resources\\static\\tximages\\";
        File dest = new File (filePath + fileName);
        File del = new File (filePath + imgtx);
        //如果要修改图片,先删除图片
        if(file!=null&&!file.equals("")) {
            if(!imgtx.equals("tx.png")) {
                boolean delete = del.delete();
            }
            furnUser.setId(Integer.valueOf((int)session.getAttribute("userid")));
            furnUser.setHeadimg(fileName);
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {

        }

        int i = registerService.updateByimgtx(furnUser);
        session.setAttribute("tximg",fileName);
        return "redirect:mygrxx.html";
    }

    /**
     * 修改资料
     */
    @PostMapping("/updatemygrxx")
    public String updatemygrxx(FurnUser furnUser,String birthday2,HttpSession session) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
        FurnUser furnUser1 = new FurnUser();
        furnUser1.setUserName(furnUser.getUserName());
        furnUser1.setGender(furnUser.getGender());
        furnUser1.setId(Integer.valueOf((int)session.getAttribute("userid")));
        furnUser1.setBirthday(formatter.parse(birthday2));
        int i = registerService.updateByimgtx(furnUser1);
        session.setAttribute("names",furnUser.getUserName());
        return "redirect:mygrxx.html";
    }


/*  判断登录账号密码*/
    @PostMapping("/index.html")
    public void register(FurnUser furnUser, HttpSession session, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        FurnUser user=registerService.login(furnUser.getUserAccount(),furnUser.getUserPassword());

        if (null==user){
            out.print("<script>alert('账户或密码错误!');location.href='login.html';</script>");
            return;
        }else {
            session.setAttribute("userid",user.getId());
            session.setAttribute("names",user.getUserName());
            session.setAttribute("paaw",user.getUserPassword());
            session.setAttribute("tximg",user.getHeadimg());
            //摆件花艺
            Object furnCommodities01 = session.getAttribute("furnCommodities");
            Object furnCommodities02 = session.getAttribute("furnCommodities2");
            if(furnCommodities01==null||furnCommodities02==null) {
                //随机6位
                List<FurnCommodity> furnCommodities1 = furnCommodityService.selectById();
                //取出所有id并存入数组
                Integer [] result = new Integer[furnCommodities1.size()];
                int i1 = 0;
                for (FurnCommodity furnCommodity:furnCommodities1) {
                    Integer id = furnCommodity.getId();
                    result[i1] = id;
                    i1++;
                }
                //创建随机数
                Random r =new Random();

                //随机个数,随机的id并存入数组
                int[]b =new int[6];
                int temp = 0;
                int index=0;
                for(int i=0;i<6;i++){
                    index =r.nextInt(result.length-i);//取a数组的下标数
                    b[i]=result[index];//把取到的数存于新建的数组b中
                    temp =result[index]; //6次循环取到的随机数按序交换到数组最后面
                    result[index]=result[result.length-1-i];
                    result[result.length-1-i]=temp;
                }
                //随机分层
                Integer [] furnId1 = new Integer[3];
                Integer [] furnId2 = new Integer[3];
                int id1 = 0;
                int id2 = 0;
                for(int i=0;i<6;i++){
                    if(i<3){
                        furnId1[id1] = b[i];
                        id1++;
                    }else{
                        furnId2[id2] = b[i];
                        id2++;
                    }
                }


                List<FurnCommodity> furnCommodities = furnCommodityService.selectByClassificationtype_idandLimit6(furnId1);
                List<FurnCommodity> furnCommodities2 = furnCommodityService.selectByClassificationtype_idandLimit6(furnId2);
                if (furnCommodities != null&&furnCommodities2 != null) {
                    session.setAttribute("furnCommodities", furnCommodities);
                    session.setAttribute("furnCommodities2", furnCommodities2);
                }
            }
            //墙壁挂画
            Object furnCommodities03 = session.getAttribute("furnCommodities3");
            Object furnCommodities04 = session.getAttribute("furnCommodities4");
            Object furnCommodities05 = session.getAttribute("furnCommodities5");
            if(furnCommodities03==null||furnCommodities04==null||furnCommodities05==null) {
                //随机16位
                List<FurnCommodity> furnCommodities = furnCommodityService.selectById2();
                //取出所有id并存入数组
                Integer [] result = new Integer[furnCommodities.size()];
                int i1 = 0;
                for (FurnCommodity furnCommodity:furnCommodities) {
                    Integer id = furnCommodity.getId();
                    result[i1] = id;
                    i1++;
                }
                //创建随机数
                Random r =new Random();

                //随机个数,随机的id并存入数组
                int[]b =new int[12];
                int temp = 0;
                int index=0;
                for(int i=0;i<12;i++){
                    index =r.nextInt(result.length-i);//取a数组的下标数
                    b[i]=result[index];//把取到的数存于新建的数组b中
                    temp =result[index]; //6次循环取到的随机数按序交换到数组最后面
                    result[index]=result[result.length-1-i];
                    result[result.length-1-i]=temp;
                }
                //随机分层
                Integer [] furnId3 = new Integer[4];
                Integer [] furnId4 = new Integer[4];
                Integer [] furnId5 = new Integer[4];
                int id3 = 0;
                int id4 = 0;
                int id5 = 0;
                for(int i=0;i<12;i++){
                    if(i<4){
                        furnId3[id3] = b[i];
                        id3++;
                    }else if(i>3&&i<8){
                        furnId4[id4] = b[i];
                        id4++;
                    } else{
                        furnId5[id5] = b[i];
                        id5++;
                    }
                }
                List<FurnCommodity> furnCommodities3 = furnCommodityService.selectByClassificationtype_parent_idandLimit16(furnId3);
                List<FurnCommodity> furnCommodities4 = furnCommodityService.selectByClassificationtype_parent_idandLimit16(furnId4);
                List<FurnCommodity> furnCommodities5 = furnCommodityService.selectByClassificationtype_parent_idandLimit16(furnId5);
                if (furnCommodities3 != null&&furnCommodities4!=null&&furnCommodities5!=null) {
                    session.setAttribute("furnCommodities3", furnCommodities3);
                    session.setAttribute("furnCommodities4", furnCommodities4);
                    session.setAttribute("furnCommodities5", furnCommodities5);
                }
            }
        }
       response.sendRedirect("index.html");
    }
    @GetMapping("/quit")
    public String quit(HttpSession session){
        session.invalidate();
        return "redirect:index.html";
    }

    @GetMapping("/cutAccount")
    public String cutAccount(HttpSession session){
        session.invalidate();
        return "redirect:login.html";
    }

    @GetMapping("/quit2")
    @ResponseBody
    public SimpleResponse quit2(HttpServletRequest request){
        try {
            FurnUser furnUser=new FurnUser();
            furnUser.setId(Integer.valueOf(request.getParameter("id")));
            furnUser.setUserPassword(request.getParameter("xmm"));
            int i=registerService.updateByPrimaryKeySelective(furnUser);
            return SimpleResponse.ok(i);
        }catch (Exception e){
            return SimpleResponse.error(9999,"修改错误","select Region");
        }
    }
}
